<html>
<head>
  <title>NPCocoaEventWindowFocusChanged Tests</title>
</head>
<body onload="runTests()">
  <embed id="plugin1" type="application/x-test" width="400" height="400"></embed>
  <embed id="plugin2" type="application/x-test" width="400" height="400"></embed>
  <script type="application/javascript">
    function is(aLeft, aRight, aMessage) {
      window.opener.SimpleTest.is(aLeft, aRight, aMessage);
    }

    function ok(aValue, aMessage) {
      window.opener.SimpleTest.ok(aValue, aMessage);
    }

    function executeSoon(func) {
      window.opener.SimpleTest.executeSoon(func);
    }

    function waitForFocus(aCb, aTarget, aBlank) {
      window.opener.SimpleTest.waitForFocus(aCb, aTarget, aBlank);
    }

    function runTests() {
      var plugin1 = document.getElementById("plugin1");
      var plugin2 = document.getElementById("plugin2");

      // Don't run any tests if we're not testing the Cocoa event model.
      if (plugin1.getEventModel() != 1) {
        window.opener.testsFinished();
        return;
      }

      // The first plugin will have in-page focus for these tests.
      plugin1.focus();

      // The expected event count which applies to all instances.
      // Initialize to 1 to account for the initial state event.
      var expectedEventCount = 1;

      // First make sure the plugins got an initial window focus event.
      // Since this window was just opened it should be in the front. If
      // the plugin has not been sent an initial window state then it will
      // be in an unknown state and it will throw an exception.
      try {
        is(plugin1.getTopLevelWindowActivationState(), true, "Activation state should be: activated");
        is(plugin1.getTopLevelWindowActivationEventCount(), expectedEventCount, "Window focus event count should be " + expectedEventCount);

        is(plugin2.getTopLevelWindowActivationState(), true, "Activation state should be: activated");
        is(plugin2.getTopLevelWindowActivationEventCount(), expectedEventCount, "Window focus event count should be " + expectedEventCount);
      } catch (e) {
        ok(false, "Plugin does not know its initial top-level window activation state!");
      }

      netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
      var fm = Components.classes["@mozilla.org/focus-manager;1"].
                          getService(Components.interfaces.nsIFocusManager);

      waitForFocus(function() {
        // Make sure the plugin handled the focus event before checking.
        executeSoon(function() {
          expectedEventCount++;

          is(plugin1.getTopLevelWindowActivationState(), false, "Activation state should be: deactivated");
          is(plugin1.getTopLevelWindowActivationEventCount(), expectedEventCount, "Window focus event count should be " + expectedEventCount);

          is(plugin2.getTopLevelWindowActivationState(), false, "Activation state should be: deactivated");
          is(plugin2.getTopLevelWindowActivationEventCount(), expectedEventCount, "Window focus event count should be " + expectedEventCount);

          // Bring our window back to the front and make sure plugins were properly notified.
          netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
          fm.focusedWindow = window;

          waitForFocus(function() {
            // Make sure the plugin handled the focus event before checking.
            executeSoon(function() {
              expectedEventCount++;

              is(plugin1.getTopLevelWindowActivationState(), true, "Activation state should be: activated");
              is(plugin1.getTopLevelWindowActivationEventCount(), expectedEventCount, "Window focus event count should be " + expectedEventCount);

              is(plugin2.getTopLevelWindowActivationState(), true, "Activation state should be: activated");
              is(plugin2.getTopLevelWindowActivationEventCount(), expectedEventCount, "Window focus event count should be " + expectedEventCount);

              window.opener.testsFinished();
            });
          }, window);
        });
      }, window.opener);

      // Send our window to the back and make sure plugins were properly notified.
      // Calling window.blur() is not allowed.
      netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
      fm.focusedWindow = window.opener;
    }
  </script>
</body>
</html>
